package com.ruoyi.cm.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.cm.domain.bo.CmBsSettleBo;
import com.ruoyi.cm.domain.vo.CmBsSettleVo;
import com.ruoyi.cm.domain.CmBsSettle;
import com.ruoyi.cm.mapper.CmBsSettleMapper;
import com.ruoyi.cm.service.ICmBsSettleService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 计量单管理Service业务层处理
 *
 * @author zyb
 * @date 2024-09-24
 */
@RequiredArgsConstructor
@Service
public class CmBsSettleServiceImpl implements ICmBsSettleService {

    private final CmBsSettleMapper baseMapper;

    /**
     * 查询计量单管理
     */
    @Override
    public CmBsSettleVo queryById(Long settleId){
        return baseMapper.selectVoById(settleId);
    }

    /**
     * 查询计量单管理列表
     */
    @Override
    public TableDataInfo<CmBsSettleVo> queryPageList(CmBsSettleBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<CmBsSettle> lqw = buildQueryWrapper(bo);
        Page<CmBsSettleVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询计量单管理列表
     */
    @Override
    public List<CmBsSettleVo> queryList(CmBsSettleBo bo) {
        LambdaQueryWrapper<CmBsSettle> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<CmBsSettle> buildQueryWrapper(CmBsSettleBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<CmBsSettle> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getProjectId() != null, CmBsSettle::getProjectId, bo.getProjectId());
        lqw.eq(bo.getContractId() != null, CmBsSettle::getContractId, bo.getContractId());
        lqw.eq(StringUtils.isNotBlank(bo.getSettleClass()), CmBsSettle::getSettleClass, bo.getSettleClass());
        lqw.eq(StringUtils.isNotBlank(bo.getSettleType()), CmBsSettle::getSettleType, bo.getSettleType());
        lqw.eq(StringUtils.isNotBlank(bo.getApprovalStatus()), CmBsSettle::getApprovalStatus, bo.getApprovalStatus());
        lqw.eq(bo.getSettleIndex() != null, CmBsSettle::getSettleIndex, bo.getSettleIndex());
        lqw.eq(StringUtils.isNotBlank(bo.getPayMode()), CmBsSettle::getPayMode, bo.getPayMode());
        lqw.eq(bo.getSettleBeginDate() != null, CmBsSettle::getSettleBeginDate, bo.getSettleBeginDate());
        lqw.eq(bo.getSettleEndDate() != null, CmBsSettle::getSettleEndDate, bo.getSettleEndDate());
        lqw.eq(bo.getSettleDate() != null, CmBsSettle::getSettleDate, bo.getSettleDate());
        lqw.eq(bo.getPayDate() != null, CmBsSettle::getPayDate, bo.getPayDate());
        lqw.eq(bo.getBqWcrl() != null, CmBsSettle::getBqWcrl, bo.getBqWcrl());
        lqw.eq(bo.getBqWczj() != null, CmBsSettle::getBqWczj, bo.getBqWczj());
        lqw.eq(bo.getBqZfyfk() != null, CmBsSettle::getBqZfyfk, bo.getBqZfyfk());
        lqw.eq(bo.getBqKcyfk() != null, CmBsSettle::getBqKcyfk, bo.getBqKcyfk());
        lqw.eq(bo.getBqLjkk() != null, CmBsSettle::getBqLjkk, bo.getBqLjkk());
        lqw.eq(bo.getBqFk() != null, CmBsSettle::getBqFk, bo.getBqFk());
        lqw.eq(bo.getJzbqyWcrl() != null, CmBsSettle::getJzbqyWcrl, bo.getJzbqyWcrl());
        lqw.eq(bo.getJzbqyWczj() != null, CmBsSettle::getJzbqyWczj, bo.getJzbqyWczj());
        lqw.eq(bo.getJzbqyZfyfk() != null, CmBsSettle::getJzbqyZfyfk, bo.getJzbqyZfyfk());
        lqw.eq(bo.getJzbqyKcyfk() != null, CmBsSettle::getJzbqyKcyfk, bo.getJzbqyKcyfk());
        lqw.eq(bo.getJzbqyLjkk() != null, CmBsSettle::getJzbqyLjkk, bo.getJzbqyLjkk());
        lqw.eq(bo.getJzbqyFk() != null, CmBsSettle::getJzbqyFk, bo.getJzbqyFk());
        lqw.eq(bo.getJzbqFkbl() != null, CmBsSettle::getJzbqFkbl, bo.getJzbqFkbl());
        lqw.eq(bo.getJzsqyWcrl() != null, CmBsSettle::getJzsqyWcrl, bo.getJzsqyWcrl());
        lqw.eq(bo.getJzsqyWczj() != null, CmBsSettle::getJzsqyWczj, bo.getJzsqyWczj());
        lqw.eq(bo.getJzsqyZfyfk() != null, CmBsSettle::getJzsqyZfyfk, bo.getJzsqyZfyfk());
        lqw.eq(bo.getJzsqyKcyfk() != null, CmBsSettle::getJzsqyKcyfk, bo.getJzsqyKcyfk());
        lqw.eq(bo.getJzsqyLjkk() != null, CmBsSettle::getJzsqyLjkk, bo.getJzsqyLjkk());
        lqw.eq(bo.getJzsqyFk() != null, CmBsSettle::getJzsqyFk, bo.getJzsqyFk());
        lqw.eq(bo.getJzsqFkbl() != null, CmBsSettle::getJzsqFkbl, bo.getJzsqFkbl());
        return lqw;
    }

    /**
     * 新增计量单管理
     */
    @Override
    public Boolean insertByBo(CmBsSettleBo bo) {
        CmBsSettle add = BeanUtil.toBean(bo, CmBsSettle.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setSettleId(add.getSettleId());
        }
        return flag;
    }

    /**
     * 修改计量单管理
     */
    @Override
    public Boolean updateByBo(CmBsSettleBo bo) {
        CmBsSettle update = BeanUtil.toBean(bo, CmBsSettle.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(CmBsSettle entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除计量单管理
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
